{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from numpy import *\n",
    "\n",
    "def createDataSet():\n",
    "    group=array([[1.1,1.2],[0.99,1.123],[0.11,0.01],[0.0,0.0]])\n",
    "    labels=['A','A','B','B']\n",
    "    return group,labels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.1    1.2  ]\n",
      " [ 0.99   1.123]\n",
      " [ 0.11   0.01 ]\n",
      " [ 0.     0.   ]]\n",
      "['A', 'A', 'B', 'B']\n",
      "4\n",
      "[[ 0.2  0.3]\n",
      " [ 0.2  0.3]\n",
      " [ 0.2  0.3]\n",
      " [ 0.2  0.3]]\n",
      "[[ 0.81      0.81    ]\n",
      " [ 0.6241    0.677329]\n",
      " [ 0.0081    0.0841  ]\n",
      " [ 0.04      0.09    ]]\n",
      "[ 1.62      1.301429  0.0922    0.13    ]\n",
      "[2 3 1 0]\n",
      "3\n",
      "[('C', 23), ('A', 3), ('B', 2)]\n",
      "[('C', 23), ('B', 2), ('A', 3)]\n",
      "[[ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]\n",
      " [ 0.  0.  0.]]\n"
     ]
    }
   ],
   "source": [
    "G,L=createDataSet()\n",
    "print(G)\n",
    "print(L)\n",
    "print(G.shape[0])#行数, shape[1]列数\n",
    "M=tile([0.2,0.3],(4,1))#以[0.2,0.3]为数据，生成4行1列的矩阵\n",
    "print(M)\n",
    "M=M-G\n",
    "print(M**2)#计算矩阵每个元素的平方\n",
    "print((M**2).sum(axis=1))#计算每一行元素的和\n",
    "M=(M**2).sum(axis=1)\n",
    "print(M.argsort())#排序,M[2],M[3],M[1],M[0]\n",
    "D={'A':3,'B':2,\"C\":23}\n",
    "print(D.get('A',0))#得到键'A'对应的值，不存在就返回0\n",
    "sortedD=sorted(D.items(), key=lambda d:d[1], reverse=True)#通过key指定按照值排序，先大\n",
    "print(sortedD)\n",
    "sortedD=sorted(D.items(), key=lambda d:d[0], reverse=True)#通过key指定按照键排序\n",
    "print(sortedD)\n",
    "mt=zeros((10,3))#10行3列的0\n",
    "print(mt)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def classify0(inX, dataSet, labels, k):\n",
    "    dataSetSize = dataSet.shape[0]#shape[0] number of rows, shape[1] number of columns\n",
    "    diffMat=tile(inX,(dataSetSize,1)) - dataSet\n",
    "    sqDiffMat=diffMat**2\n",
    "    sqDist=sqDiffMat.sum(axis=1)\n",
    "    dist=sqDist**0.5\n",
    "    sortedDistIndicies=dist.argsort()\n",
    "    \n",
    "    classCount={}\n",
    "    for i in range(k):\n",
    "        voteIlabel=labels[sortedDistIndicies[i]]\n",
    "        classCount[voteIlabel]=classCount.get(voteIlabel,0)+1\n",
    "    \n",
    "    sortedClassCount=sorted(classCount.items(),key=lambda d:d[1],reverse=True)\n",
    "    return sortedClassCount[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n"
     ]
    }
   ],
   "source": [
    "print(classify0([0.21,0.23], G, L, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "an integer is required (got type str)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-58-e0ad1ec71b1b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'datingTestSet.txt'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'r'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mline\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mline\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstrip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mlistFromLine\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mline\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'\\t'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mmat\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mzeros\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: an integer is required (got type str)"
     ]
    }
   ],
   "source": [
    "fr = open('datingTestSet.txt')\n",
    "line=fr.readline()\n",
    "line=line.strip()\n",
    "listFromLine=line.split('\\t')\n",
    "mat=zeros((2,3))\n",
    "mat[0,:]=listFromLine[0:3]\n",
    "print(listFromLine[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  4.09200000e+04   8.32697600e+00   9.53952000e-01]\n",
      " [  1.44880000e+04   7.15346900e+00   1.67390400e+00]\n",
      " [  2.60520000e+04   1.44187100e+00   8.05124000e-01]\n",
      " [  7.51360000e+04   1.31473940e+01   4.28964000e-01]\n",
      " [  3.83440000e+04   1.66978800e+00   1.34296000e-01]\n",
      " [  7.29930000e+04   1.01417400e+01   1.03295500e+00]\n",
      " [  3.59480000e+04   6.83079200e+00   1.21319200e+00]\n",
      " [  4.26660000e+04   1.32763690e+01   5.43880000e-01]\n",
      " [  6.74970000e+04   8.63157700e+00   7.49278000e-01]\n",
      " [  3.54830000e+04   1.22731690e+01   1.50805300e+00]\n",
      " [  5.02420000e+04   3.72349800e+00   8.31917000e-01]\n",
      " [  6.32750000e+04   8.38587900e+00   1.66948500e+00]\n",
      " [  5.56900000e+03   4.87543500e+00   7.28658000e-01]\n",
      " [  5.10520000e+04   4.68009800e+00   6.25224000e-01]\n",
      " [  7.73720000e+04   1.52995700e+01   3.31351000e-01]\n",
      " [  4.36730000e+04   1.88946100e+00   1.91283000e-01]\n",
      " [  6.13640000e+04   7.51675400e+00   1.26916400e+00]\n",
      " [  6.96730000e+04   1.42391950e+01   2.61333000e-01]\n",
      " [  1.56690000e+04   0.00000000e+00   1.25018500e+00]\n",
      " [  2.84880000e+04   1.05285550e+01   1.30484400e+00]\n",
      " [  6.48700000e+03   3.54026500e+00   8.22483000e-01]\n",
      " [  3.77080000e+04   2.99155100e+00   8.33920000e-01]\n",
      " [  2.26200000e+04   5.29786500e+00   6.38306000e-01]\n",
      " [  2.87820000e+04   6.59380300e+00   1.87108000e-01]\n",
      " [  1.97390000e+04   2.81676000e+00   1.68620900e+00]\n",
      " [  3.67880000e+04   1.24582580e+01   6.49617000e-01]\n",
      " [  5.74100000e+03   0.00000000e+00   1.65641800e+00]\n",
      " [  2.85670000e+04   9.96864800e+00   7.31232000e-01]\n",
      " [  6.80800000e+03   1.36483800e+00   6.40103000e-01]\n",
      " [  4.16110000e+04   2.30453000e-01   1.15199600e+00]]\n",
      "['largeDoses', 'smallDoses', 'didntLike', 'didntLike', 'didntLike', 'didntLike', 'largeDoses', 'largeDoses', 'didntLike', 'largeDoses', 'didntLike', 'didntLike', 'smallDoses', 'didntLike', 'didntLike', 'didntLike', 'didntLike', 'didntLike', 'smallDoses', 'largeDoses']\n"
     ]
    }
   ],
   "source": [
    "def file2matrix(filename):\n",
    "    fr = open(filename)\n",
    "    numberOfLines=len(fr.readlines())\n",
    "    retMat=zeros((numberOfLines,3))\n",
    "    classLabelVector=[]\n",
    "    fr = open(filename,'r',encoding='utf-8')\n",
    "    for i,line in enumerate(fr.readlines()):\n",
    "        line=line.strip()\n",
    "        listFromLine=line.split('\\t')\n",
    "        retMat[i,:]=listFromLine[0:3]\n",
    "        classLabelVector.append((listFromLine[-1]))\n",
    "    return retMat,classLabelVector\n",
    "\n",
    "datingDataMat,datingLabels=file2matrix('datingTestSet.txt')\n",
    "print(datingDataMat[0:30,:])\n",
    "print(datingLabels[0:20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "mat=[[1,2,3],\n",
    "    [4,5,6]]\n",
    "mat=array(mat)#转化为矩阵才可以用min,max\n",
    "minVals=mat.min(0)#得到每一列的最小值, min(1)得到每一行的最小值\n",
    "print(minVals)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.44832535,  0.39805139,  0.56233353],\n",
       "       [ 0.15873259,  0.34195467,  0.98724416],\n",
       "       [ 0.28542943,  0.06892523,  0.47449629],\n",
       "       ..., \n",
       "       [ 0.29115949,  0.50910294,  0.51079493],\n",
       "       [ 0.52711097,  0.43665451,  0.4290048 ],\n",
       "       [ 0.47940793,  0.3768091 ,  0.78571804]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def autoNorm(dataSet):\n",
    "    minVals = dataSet.min(0)\n",
    "    maxVals = dataSet.max(0)\n",
    "    ranges=maxVals-minVals\n",
    "    mormDataSet=zeros(shape(dataSet))\n",
    "    m=dataSet.shape[0]\n",
    "    normDataSet=dataSet-tile(minVals,(m,1))\n",
    "    normDataSet=normDataSet/tile(ranges,(m,1))\n",
    "    return normDataSet,ranges,minVals\n",
    "normMat,ranges,minVals=autoNorm(datingDataMat)\n",
    "normMat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  9.12730000e+04,   2.09193490e+01,   1.69436100e+00])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ranges"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.      ,  0.      ,  0.001156])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "minVals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0th test result largeDoses, the real answer is largeDoses\n",
      "1th test result smallDoses, the real answer is smallDoses\n",
      "2th test result didntLike, the real answer is didntLike\n",
      "3th test result didntLike, the real answer is didntLike\n",
      "4th test result didntLike, the real answer is didntLike\n",
      "5th test result didntLike, the real answer is didntLike\n",
      "6th test result largeDoses, the real answer is largeDoses\n",
      "7th test result largeDoses, the real answer is largeDoses\n",
      "8th test result didntLike, the real answer is didntLike\n",
      "9th test result largeDoses, the real answer is largeDoses\n",
      "10th test result didntLike, the real answer is didntLike\n",
      "11th test result didntLike, the real answer is didntLike\n",
      "12th test result smallDoses, the real answer is smallDoses\n",
      "13th test result didntLike, the real answer is didntLike\n",
      "14th test result didntLike, the real answer is didntLike\n",
      "15th test result didntLike, the real answer is didntLike\n",
      "16th test result didntLike, the real answer is didntLike\n",
      "17th test result didntLike, the real answer is didntLike\n",
      "18th test result smallDoses, the real answer is smallDoses\n",
      "19th test result largeDoses, the real answer is largeDoses\n",
      "20th test result smallDoses, the real answer is smallDoses\n",
      "21th test result didntLike, the real answer is didntLike\n",
      "22th test result largeDoses, the real answer is smallDoses\n",
      "23th test result largeDoses, the real answer is largeDoses\n",
      "24th test result smallDoses, the real answer is smallDoses\n",
      "25th test result largeDoses, the real answer is largeDoses\n",
      "26th test result smallDoses, the real answer is smallDoses\n",
      "27th test result largeDoses, the real answer is largeDoses\n",
      "28th test result smallDoses, the real answer is smallDoses\n",
      "29th test result didntLike, the real answer is didntLike\n",
      "30th test result largeDoses, the real answer is largeDoses\n",
      "31th test result didntLike, the real answer is didntLike\n",
      "32th test result largeDoses, the real answer is largeDoses\n",
      "33th test result didntLike, the real answer is didntLike\n",
      "34th test result smallDoses, the real answer is smallDoses\n",
      "35th test result didntLike, the real answer is didntLike\n",
      "36th test result didntLike, the real answer is didntLike\n",
      "37th test result smallDoses, the real answer is smallDoses\n",
      "38th test result largeDoses, the real answer is largeDoses\n",
      "39th test result largeDoses, the real answer is largeDoses\n",
      "40th test result didntLike, the real answer is didntLike\n",
      "41th test result smallDoses, the real answer is smallDoses\n",
      "42th test result largeDoses, the real answer is largeDoses\n",
      "43th test result largeDoses, the real answer is largeDoses\n",
      "44th test result largeDoses, the real answer is largeDoses\n",
      "45th test result didntLike, the real answer is didntLike\n",
      "46th test result didntLike, the real answer is didntLike\n",
      "47th test result didntLike, the real answer is didntLike\n",
      "48th test result didntLike, the real answer is didntLike\n",
      "49th test result smallDoses, the real answer is smallDoses\n",
      "50th test result smallDoses, the real answer is smallDoses\n",
      "51th test result didntLike, the real answer is didntLike\n",
      "52th test result largeDoses, the real answer is largeDoses\n",
      "53th test result smallDoses, the real answer is smallDoses\n",
      "54th test result smallDoses, the real answer is smallDoses\n",
      "55th test result smallDoses, the real answer is smallDoses\n",
      "56th test result smallDoses, the real answer is smallDoses\n",
      "57th test result largeDoses, the real answer is largeDoses\n",
      "58th test result didntLike, the real answer is didntLike\n",
      "59th test result smallDoses, the real answer is smallDoses\n",
      "60th test result didntLike, the real answer is didntLike\n",
      "61th test result smallDoses, the real answer is smallDoses\n",
      "62th test result smallDoses, the real answer is smallDoses\n",
      "63th test result smallDoses, the real answer is smallDoses\n",
      "64th test result smallDoses, the real answer is smallDoses\n",
      "65th test result smallDoses, the real answer is smallDoses\n",
      "66th test result largeDoses, the real answer is largeDoses\n",
      "67th test result smallDoses, the real answer is smallDoses\n",
      "68th test result largeDoses, the real answer is largeDoses\n",
      "69th test result didntLike, the real answer is didntLike\n",
      "70th test result smallDoses, the real answer is smallDoses\n",
      "71th test result largeDoses, the real answer is largeDoses\n",
      "72th test result smallDoses, the real answer is smallDoses\n",
      "73th test result smallDoses, the real answer is smallDoses\n",
      "74th test result largeDoses, the real answer is didntLike\n",
      "75th test result largeDoses, the real answer is largeDoses\n",
      "76th test result didntLike, the real answer is didntLike\n",
      "77th test result didntLike, the real answer is didntLike\n",
      "78th test result largeDoses, the real answer is largeDoses\n",
      "79th test result largeDoses, the real answer is largeDoses\n",
      "80th test result didntLike, the real answer is didntLike\n",
      "81th test result smallDoses, the real answer is smallDoses\n",
      "82th test result largeDoses, the real answer is largeDoses\n",
      "83th test result largeDoses, the real answer is didntLike\n",
      "84th test result largeDoses, the real answer is largeDoses\n",
      "85th test result didntLike, the real answer is didntLike\n",
      "86th test result smallDoses, the real answer is smallDoses\n",
      "87th test result smallDoses, the real answer is smallDoses\n",
      "88th test result didntLike, the real answer is didntLike\n",
      "89th test result didntLike, the real answer is didntLike\n",
      "90th test result largeDoses, the real answer is largeDoses\n",
      "91th test result smallDoses, the real answer is largeDoses\n",
      "92th test result didntLike, the real answer is didntLike\n",
      "93th test result smallDoses, the real answer is smallDoses\n",
      "94th test result didntLike, the real answer is didntLike\n",
      "95th test result largeDoses, the real answer is largeDoses\n",
      "96th test result largeDoses, the real answer is largeDoses\n",
      "97th test result smallDoses, the real answer is smallDoses\n",
      "98th test result didntLike, the real answer is didntLike\n",
      "99th test result largeDoses, the real answer is didntLike\n",
      "the total error rate is 0.050000\n"
     ]
    }
   ],
   "source": [
    "def datingTest():\n",
    "    hoRatio=0.10\n",
    "    datingDataMat,datingLabels=file2matrix('datingTestSet.txt')\n",
    "    normMat, ranges, minVals=autoNorm(datingDataMat)\n",
    "    m=normMat.shape[0]\n",
    "    numTestVecs=int(m*hoRatio)\n",
    "    errorCount=0.0\n",
    "    for i in range(numTestVecs):\n",
    "        classifierResult=classify0(normMat[i,:],normMat[numTestVecs:m,:],\\\n",
    "                                 datingLabels[numTestVecs:m],3)\n",
    "        print(\"%dth test result %s, the real answer is %s\" %\\\n",
    "              (i,classifierResult,datingLabels[i]))\n",
    "        if(classifierResult!=datingLabels[i]):\n",
    "            errorCount+=1.0\n",
    "    print (\"the total error rate is %f\" % (errorCount/float(numTestVecs)))\n",
    "datingTest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "percentage of time spent playing video games? 10\n",
      "frequent flier miles earned per years? 10000\n",
      "liters of ice cream consumed per years? 0.5\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "an integer is required (got type str)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-50-597830bd3b6c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[0mresult\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mclassify0\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minArr\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mminVals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mranges\u001b[0m\u001b[1;33m,\u001b[0m                    \u001b[0mnormMat\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdatingLabels\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mclassifyPerson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-50-597830bd3b6c>\u001b[0m in \u001b[0;36mclassifyPerson\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mffMiles\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m                           \u001b[1;34m\"frequent flier miles earned per years? \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0miceCream\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m                            \u001b[1;34m\"liters of ice cream consumed per years? \"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m     \u001b[0mdatingDataMat\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdatingLabels\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfile2matrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'datingTestSet.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m     \u001b[0mnormMat\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mranges\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mminVals\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mautoNorm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdatingDataMat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[0minArr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mffMiles\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mpercentT\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0miceCream\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-8-240be8d9898b>\u001b[0m in \u001b[0;36mfile2matrix\u001b[1;34m(filename)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfile2matrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mfr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'r'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'utf-8'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m     \u001b[0mnumberOfLines\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mretMat\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mzeros\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumberOfLines\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mclassLabelVector\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: an integer is required (got type str)"
     ]
    }
   ],
   "source": [
    "def classifyPerson():\n",
    "    percentT=float(input(\\\n",
    "                            \"percentage of time spent playing video games? \"))\n",
    "    ffMiles=float(input(\\\n",
    "                           \"frequent flier miles earned per years? \"))\n",
    "    iceCream=float(input(\\\n",
    "                            \"liters of ice cream consumed per years? \"))\n",
    "    datingDataMat,datingLabels=file2matrix('datingTestSet.txt')\n",
    "    normMat,ranges,minVals=autoNorm(datingDataMat)\n",
    "    inArr=array([ffMiles,percentT,iceCream])\n",
    "    result=classify0((inArr-minVals)/ranges,\\\n",
    "                    normMat,datingLabels,3)\n",
    "    print(result)\n",
    "classifyPerson()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 0.,  0.])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=zeros((2,4))\n",
    "print(a[0,0])#表示第一行第一列的元素\n",
    "print(a[0])#表示第一行的元素\n",
    "a[:,0]#表示第一列的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "an integer is required (got type str)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-49-6b74f1993262>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      7\u001b[0m             \u001b[0mretVec\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m32\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlineStr\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mretVec\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mtestVec\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mimg2vec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'testDigits/0_13.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     10\u001b[0m \u001b[0mtestVec\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m30\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-49-6b74f1993262>\u001b[0m in \u001b[0;36mimg2vec\u001b[1;34m(filename)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mimg2vec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mretVec\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mzeros\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1024\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0mfr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'r'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'utf-8'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m32\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[0mlineStr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: an integer is required (got type str)"
     ]
    }
   ],
   "source": [
    "def img2vec(filename):\n",
    "    retVec=zeros((1,1024))\n",
    "    fr=open(filename,'r',encoding='utf-8')\n",
    "    for i in range(32):\n",
    "        lineStr=fr.readline()\n",
    "        for j in range(32):\n",
    "            retVec[0,32*i+j]=int(lineStr[j])\n",
    "    return retVec\n",
    "testVec=img2vec('testDigits/0_13.txt')\n",
    "testVec[0,0:30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "an integer is required (got type str)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-46-475c16354f74>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     26\u001b[0m             \u001b[0merrorCount\u001b[0m\u001b[1;33m+=\u001b[0m\u001b[1;36m1.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     27\u001b[0m     \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"the total error rate is %f\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0merrorCount\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmTest\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 28\u001b[1;33m \u001b[0mhandwritingClassTest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-46-475c16354f74>\u001b[0m in \u001b[0;36mhandwritingClassTest\u001b[1;34m()\u001b[0m\n\u001b[0;32m     11\u001b[0m         \u001b[0mclassNumStr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfileStr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'_'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[0mhwLabels\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mclassNumStr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m         \u001b[0mtraingMat\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mimg2vec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'trainingDigits/%s'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mfileNameStr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     14\u001b[0m     \u001b[0mtestFileList\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'testingDigits'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m     \u001b[0merrorCount\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-41-6b74f1993262>\u001b[0m in \u001b[0;36mimg2vec\u001b[1;34m(filename)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mimg2vec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mretVec\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mzeros\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1024\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0mfr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'r'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'utf-8'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m32\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[0mlineStr\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mfr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: an integer is required (got type str)"
     ]
    }
   ],
   "source": [
    "def handwritingClassTest():\n",
    "    hwLabels=[]\n",
    "    trainingFileList=listdir('trainingDigits')\n",
    "    m=len(trainingFileList)\n",
    "    trainingMat=zeros((m,1024))\n",
    "    for i in range(m):\n",
    "        fileNameStr=trainingFileList[i]\n",
    "        fileStr=fileNameStr.split('.')[0]\n",
    "        classNumStr=int(fileStr.split('_')[0])\n",
    "        hwLabels.append(classNumStr)\n",
    "        traingMat[i,:]=img2vec('trainingDigits/%s' % fileNameStr)\n",
    "    testFileList=listdir('testingDigits')\n",
    "    errorCount=0.0\n",
    "    mTest=len(testFileList)\n",
    "    for i in range(mTest):\n",
    "        fileNameStr=testFileList[i]\n",
    "        fileStr=fileNameStr.split('.')[0]\n",
    "        classNumStr=int(fileStr.split('_')[0])\n",
    "        vecUnderTest=img2vec('testDigits/%s' % fileNameStr)\n",
    "        classifierResult=classify0(vecUnderTest,\n",
    "                                  trainingMat,hwLabels,3)\n",
    "        print(\"%dth test result %s, the real answer is %s\" %\\\n",
    "              (i,classifierResult,classNumStr))\n",
    "        if(classifierResult!=classNumStr):\n",
    "            errorCount+=1.0\n",
    "    print (\"the total error rate is %f\" % (errorCount/float(mTest))) \n",
    "handwritingClassTest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
